using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Deepcode.Flux.Core.Data;

namespace Deepcode.Flux.Core.Systems.Workflow.Bases
{
	/// <summary>
	/// Manager class for the workflow processes start actions within a process
	/// </summary>
	public class Taskings : BaseManager
	{
		#region Constructors
		public Taskings() : base() { }
		public Taskings(SqlConnection CurrentConnection) : base(CurrentConnection) { }
		public Taskings(SqlConnection CurrentConnection, SqlTransaction CurrentTransaction) : base(CurrentConnection, CurrentTransaction) { }
		#endregion
		#region Data Retrieval
		/// <summary>
		/// Get all taskings associated with a process
		/// </summary>
		/// <param name="ProcessID"></param>
		/// <returns></returns>
		public Tasking[] GetTaskingsByProcessID(int ProcessID)
		{
			DataTable results = new DataTable();

			EstablishConnection();
			SqlDataAdapter da = this.CreateDataAdapter("coreWFTaskingGetByProcessID");
			da.SelectCommand.Parameters.Add(SQLUtility.ParameterIn("@id", SqlDbType.Int, 4, ProcessID));
			da.Fill(results);
			ReleaseConnection();

			List<Tasking> list = new List<Tasking>();
			foreach (DataRow r in results.Rows)
				list.Add(new Tasking(r));

			return list.ToArray();
		}
		/// <summary>
		/// Gets a tasking by it's own ID
		/// </summary>
		/// <param name="ID"></param>
		/// <returns></returns>
		public Tasking GetTaskingByID(int ID)
		{
			DataTable result = new DataTable();

			EstablishConnection();
			SqlDataAdapter da = this.CreateDataAdapter("coreWFTaskingGetByID");
			da.SelectCommand.Parameters.Add(SQLUtility.ParameterIn("@id", SqlDbType.Int, 4, ID));
			da.Fill(result);
			ReleaseConnection();

			if (result == null) return null;
			if (result.Rows.Count < 1) return null;

			return new Tasking(result.Rows[0]);
		}
		#endregion
		#region Save Records
		/// <summary>
		/// Save the specified tasking to the database
		/// </summary>
		/// <param name="ps"></param>
		/// <returns></returns>
		public int Save(Tasking t)
		{
			EstablishConnection();
			SqlCommand cmd = this.CreateSqlCommand("coreWFTaskingSave");
			SqlParameter result = SQLUtility.ParameterInOut("@id", SqlDbType.Int, 4, t.PK_ID);
			cmd.Parameters.Add(result);

			cmd.Parameters.Add( SQLUtility.ParameterIn("@FK_ProcessID", SqlDbType.Int, 4, t.FK_ProcessID));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@TaskCaption", SqlDbType.NVarChar, 500, t.TaskCaption));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@CSV_OperatorID", SqlDbType.NVarChar, 500, t.CSV_OperatorID));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@CSV_GroupID", SqlDbType.NVarChar, 500, t.CSV_GroupID ));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@GetUserMethod", SqlDbType.NVarChar, 500, t.GetUserMethod));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@SystemTask", SqlDbType.Bit, 1, t.SystemTask ));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@SystemExitAt", SqlDbType.Int, 4, t.SystemExitAt ));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@SystemExitAtUnit", SqlDbType.Int, 4, (int) t.SystemExitAtUnit ));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@ActivateTaskAt", SqlDbType.Int, 4, t.ActivateTaskAt ));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@ActivateTaskAtUnit", SqlDbType.Int, 4, (int) t.ActivateTaskAtUnit ));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@DeadlineTaskAt", SqlDbType.Int, 4, t.DeadlineTaskAt ));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@DeadlineTaskAtUnit", SqlDbType.Int, 4, (int) t.DeadlineTaskAtUnit ));
			
			cmd.ExecuteNonQuery();
			ReleaseConnection();

			if (t.PK_ID == -1)
				t.PK_ID = (int)result.Value;

			return t.PK_ID;
		}
		#endregion
		#region Delete Records
		/// <summary>
		/// Delete the specified tasking from the database
		/// </summary>
		/// <param name="ID"></param>
		/// <returns></returns>
		public bool DeleteByID(int ID)
		{
			EstablishConnection();
			SqlCommand cmd = this.CreateSqlCommand("coreWFTaskingDeleteByID");
			cmd.Parameters.Add(SQLUtility.ParameterIn("@id", SqlDbType.Int, 4, ID));
			cmd.ExecuteNonQuery();
			ReleaseConnection();
			return true;
		}
		#endregion
	}
}
